package com.cheyun.netsalev3.view

import android.content.Context
import android.widget.Switch
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.ActivityNewsSetBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.NewsSetActivityViewModel
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.core.app.NotificationManagerCompat
import com.cheyun.netsalev3.MyApplication
import com.cheyun.netsalev3.utils.CommonFunc


class NewsSetActivity : BaseActivity<ActivityNewsSetBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_news_set
    }
    var context: Context? = null
    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getNewsSetActivityViewModelFactory()
        return ViewModelProviders.of(this, factory).get(NewsSetActivityViewModel::class.java)
    }

    override fun initView() {
        binding.switchView1.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView is Switch) {
                onCheckedChangeListener(buttonView)
            }
        }

        binding.switchView2.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView is Switch) {
                onCheckedChangeListener(buttonView)
            }
        }
        binding.switchView3.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView is Switch) {
                onCheckedChangeListener(buttonView)
            }
        }
        binding.switchView4.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView is Switch) {
                onCheckedChangeListener(buttonView)
            }
        }
        binding.switchView5.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView is Switch) {
                onCheckedChangeListener(buttonView)
            }
        }
        context = this
        binding.rlAuto.setOnClickListener {
            var intent = CommonFunc.getAutostartSettingIntent(this)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                startActivity(intent)
            } catch (e: Exception) {//抛出异常就直接打开设置页面
                intent = Intent(Settings.ACTION_SETTINGS)
                startActivity(intent)
            }

        }
        if (NotificationManagerCompat.from(MyApplication.getInstance().baseContext).areNotificationsEnabled()) {
            binding.txtNotice.text = "已开启"
        }else{
            binding.txtNotice.text = "未开启"
            binding.rlSysNotice.setOnClickListener {
                checkNotificationsEnabled()
            }
        }


    }

    fun onCheckedChangeListener(buttonView: Switch) {
        if (!boolean) return
        when (buttonView.id) {
            R.id.switchView1 -> binding.viewModel?.onClickView(buttonView, "system")
            R.id.switchView2 -> binding.viewModel?.onClickView(buttonView, "group")
            R.id.switchView3 -> binding.viewModel?.onClickView(buttonView, "receive")
            R.id.switchView4 -> binding.viewModel?.onClickView(buttonView, "change")
            R.id.switchView5 -> binding.viewModel?.onClickView(buttonView, "share")
        }
    }

    //是否需要监听
    var boolean = true

    override fun initData() {
        binding.viewModel?.notice?.observe(this, Observer {
            boolean = false
            binding.switchView1.isChecked = it.system == "1"
            binding.switchView2.isChecked = it.group == "1"
            binding.switchView3.isChecked = it.receive == "1"
            binding.switchView4.isChecked = it.change == "1"
            binding.switchView5.isChecked = it.share == "1"
            boolean = true
        })
    }

    private fun checkNotificationsEnabled(){
        if (!NotificationManagerCompat.from(MyApplication.getInstance().baseContext).areNotificationsEnabled()) {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <  Build.VERSION_CODES.O) {
                var intent = Intent()
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS")
                intent.putExtra("app_package", MyApplication.getInstance().packageName)
                intent.putExtra("app_uid", context?.getApplicationInfo()?.uid)
                startActivity(intent);
            } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                var intent = Intent()
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + MyApplication.getInstance().packageName));
                startActivity(intent);
            } else {
                var localIntent = Intent()
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                localIntent.setData(Uri.fromParts("package", MyApplication.getInstance().packageName, null));
                startActivity(localIntent);
            }
        }else{
//            var win = getWindow()
//            win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
        }
    }

}
